<template>
  <div>
    <el-button
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      type="primary"
      @click="toAddSpuValue"
      >添加SPU</el-button
    >
    <el-table :data="spuList" border style="margin: 20px 0">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <el-button
            size="small"
            type="primary"
            title="添加SKU"
            :icon="Plus"
            @click="toAddSkuInfo(row)"
          ></el-button>
          <el-button
            size="small"
            type="primary"
            title="修改SPU"
            :icon="Edit"
            @click="toEditSpuInfo(row)"
          ></el-button>
          <el-button
            size="small"
            type="info"
            title="查看SKU列表"
            :icon="InfoFilled"
            @click="toCheckSkuList(row.id)"
          ></el-button>
          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            :icon="InfoFilled"
            icon-color="orange"
            title="确定要删除此属性值吗?"
            @confirm="deleteSpuInfo(row.id)"
          >
            <template #reference>
              <el-button
                title="删除"
                size="small"
                type="danger"
                :icon="Delete"
              />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 弹出框列表 -->
    <el-dialog v-model="dialogSkuTableVisible" title="查看Sku数据列表">
      <el-table :data="skuList" v-loading="loading">
        <el-table-column label="名称" prop="skuName" />
        <el-table-column label="价格(元)" prop="price" />
        <el-table-column label="重量(KG)" prop="weight" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <el-image
              :src="row.skuDefaultImg"
              style="width: 100px; height: 100px"
            ></el-image>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script lang="ts">
export default {
  name: 'SpuList',
}
</script>
<script lang="ts" setup>
// 引入接口函数
import { reqDeleteSpuInfo, reqGetSpuListData } from '@/api/product/spu'
import { reqGetSkuListData } from '@/api/product/sku'
import { useCategoryListStore } from '@/stores/categoryList'
import { ref, watch } from 'vue'
import { Plus, Edit, Delete, InfoFilled } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
const emits = defineEmits(['changeScene', 'getSpuInfo'])

const skuList = ref([])
const dialogSkuTableVisible = ref(false)
const loading = ref(false)

const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const categoryStore = useCategoryListStore()
// 分页相关的变量
const currentPage = ref<number>(1)
const pageSize = ref<number>(3)
const total = ref<number>(1)

const spuList = ref([])

// 1. 定义一个获取spu列表数据的函数 后期分页或是添加等都会用到
const getSpuListData = async () => {
  const result = await reqGetSpuListData(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id
  )
  console.log('spu', result)
  spuList.value = result.records
  total.value = result.total
}

// 2. 监听三级导航数据  只要三级导航一变化 就立即发送请求
watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      // 如果没有三级分类的id
      spuList.value = [] // 清空列表页
      return //阻止代码继续向下执行
    }
    //如果有三级分类的id 才发送请求，获取数据
    getSpuListData()
  },
  { immediate: true }
)

// 分页相关的两个事件
// 3. 更改每页显示条数时触发的事件处理程序
const handleSizeChange = (val: number) => {
  // val就是每页显示的条数 其实也相当于是pageSize
  pageSize.value = val
  currentPage.value = 1
  // 重新获取页面列表数据
  getSpuListData()
}

// 4. 根据当前页码进行切换时触发的函数
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`)
  currentPage.value = val
  getSpuListData()
}

// 5. 删除spuInfo
const deleteSpuInfo = async (id: number) => {
  await reqDeleteSpuInfo(id)

  // 提示一下用户  删除成功
  ElMessage.success('删除SPU成功')

  // 重新渲染一下剩余数据
  getSpuListData()
}

// 6. 编辑SpuInfo 之数据回显
const toEditSpuInfo = (row) => {
  // 跳转到spuForm.vue页面
  emits('changeScene', 2)
  // 将当前待编辑的数据传递给父组件index.vue
  // console.log('row',row)
  // emits('getSpuInfo',row)
  // 将数据传过去
  emits('getSpuInfo', {
    ...row,
    spuImageList: [], // 把缺少的两项，手动补上
    spuSaleAttrList: [],
  })
}

// 7. 去添加spuInfo
const toAddSpuValue = () => {
  // 跳转到spuForm页面
  emits('changeScene', 2)
  // 清空原来的数据
  emits('getSpuInfo', {})
}

// 8. 去添加SkuInfo
const toAddSkuInfo = (row) => {
  // 实现页面的切换
  emits('changeScene', 3)
  // 将数据传递过去
  emits('getSpuInfo', row)
}

// 9. 查看skuList
const toCheckSkuList = async (id: number) => {
  dialogSkuTableVisible.value = true
  loading.value = true
  skuList.value = await reqGetSkuListData(id)

  // 去掉loading
  loading.value = false
}
</script>

<style></style>
